package com.example.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pojo.CusDevPlan;
import com.example.mapper.CusDevPlanMapper;
import com.example.service.ICusDevPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.util.AssertUtil;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jayway.jsonpath.internal.Utils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *              客户开发计划
 * @author wsl
 * @since 2022-04-13
 */
@Service
public class CusDevPlanServiceImpl extends ServiceImpl<CusDevPlanMapper, CusDevPlan> implements ICusDevPlanService {
    @Autowired
    private CusDevPlanMapper cusDevPlanMapper;


    /**
     * 查询所有
     */
    public List<CusDevPlan> selectAll(){
        QueryWrapper<CusDevPlan> wrapper = new QueryWrapper<CusDevPlan>().orderByDesc("id");
        return cusDevPlanMapper.selectList(wrapper);
    }

    /**
     * 添加计划项
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public ResultVo addPlan(CusDevPlan cusDevPlan){
        //校验参数
        checkPlan(cusDevPlan);
        //isValid是否有效      (0=无效，1=有效)  设置为有效1=有效
        cusDevPlan.setIsValid(1);
        //createDate创建时间    默认为当前系统时间
        cusDevPlan.setCreateDate(new Date());
        //updateDate更新时间    默认为当前系统时间
        cusDevPlan.setUpdateDate(new Date());
        int i = baseMapper.insert(cusDevPlan);
        return i > 0 ? ResultVo.ok("添加成功！") : ResultVo.error("添加失败！");
    }
    //校验参数
    private void checkPlan(CusDevPlan cusDevPlan){
        //计划项planItem        非空
        AssertUtil.isTrue(Utils.isEmpty(cusDevPlan.getPlanItem()),"计划项内容不能为空！");
        //计划时间
        AssertUtil.isTrue(null==cusDevPlan.getPlanDate(),"计划日期不能为空！");
    }


    /**
     * 无条件分页
     * @return
     */
    public PageInfo<CusDevPlan> electByPager(Integer pageNo, Integer pageSize){
        PageHelper.startPage(pageNo,pageSize);
        QueryWrapper<CusDevPlan> wrapper=new QueryWrapper<CusDevPlan>().orderByDesc("id");
        List<CusDevPlan> list=baseMapper.selectList(wrapper);
        return new PageInfo<>(list);
    }
}
